package com.tokenplus.thai.components.bar

import android.annotation.SuppressLint
import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.widget.LinearLayout
import androidx.fragment.app.FragmentManager
import com.tokenplus.thai.R
import com.tokenplus.thai.databinding.ViewBottomTabBarBinding

class BottomTabBar : LinearLayout {

    lateinit var mViewBinding: ViewBottomTabBarBinding
    private lateinit var vRootView: View

    private var mSupportFragmentManager: FragmentManager? = null

    private var mOnActionClickListener: (Int) -> Unit = {}

    constructor(context: Context?) : this(context, null)
    constructor(context: Context?, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : this(
        context,
        attrs,
        defStyleAttr,
        0
    )

    @SuppressLint("Recycle")
    constructor(
        context: Context?,
        attrs: AttributeSet?,
        defStyleAttr: Int,
        defStyleRes: Int,
    ) : super(
        context,
        attrs,
        defStyleAttr,
        defStyleRes
    ) {
        context?.let {
            vRootView =
                View.inflate(it, R.layout.view_bottom_tab_bar, this)
            mViewBinding =
                ViewBottomTabBarBinding.bind(vRootView)
            initSubViews()
        }
    }

    private fun initSubViews() {
        try {
            mViewBinding.bottomTabBarItem0.setOnActionClickListener {
                mViewBinding.bottomTabBarItem0.setCheck(true)
                mViewBinding.bottomTabBarItem1.setCheck(false)
                mViewBinding.bottomTabBarItem2.setCheck(false)
                mOnActionClickListener.invoke(0)
            }

            mViewBinding.bottomTabBarItem1.setOnActionClickListener {
                mViewBinding.bottomTabBarItem0.setCheck(false)
                mViewBinding.bottomTabBarItem1.setCheck(true)
                mViewBinding.bottomTabBarItem2.setCheck(false)
                mOnActionClickListener.invoke(1)
            }

            mViewBinding.bottomTabBarItem2.setOnActionClickListener {
                mViewBinding.bottomTabBarItem0.setCheck(false)
                mViewBinding.bottomTabBarItem1.setCheck(false)
                mViewBinding.bottomTabBarItem2.setCheck(true)
                mOnActionClickListener.invoke(2)
            }
        } catch (e: java.lang.Exception) {
            e.printStackTrace()
        }
    }

    fun setFragmentManager(manager: FragmentManager) {
        mSupportFragmentManager = manager
    }

    fun setOnActionClickListener(listener: (Int) -> Unit) {
        mOnActionClickListener = listener
    }

    fun setTabClick(index: Int) {
        when (index) {
            0 -> {
                mViewBinding.bottomTabBarItem0.setCheck(true)
                mViewBinding.bottomTabBarItem1.setCheck(false)
                mViewBinding.bottomTabBarItem2.setCheck(false)
                mOnActionClickListener.invoke(0)
            }

            1 -> {
                mViewBinding.bottomTabBarItem0.setCheck(false)
                mViewBinding.bottomTabBarItem1.setCheck(true)
                mViewBinding.bottomTabBarItem2.setCheck(false)
                mOnActionClickListener.invoke(1)
            }

            2 -> {
                mViewBinding.bottomTabBarItem0.setCheck(false)
                mViewBinding.bottomTabBarItem1.setCheck(false)
                mViewBinding.bottomTabBarItem2.setCheck(true)
                mOnActionClickListener.invoke(2)
            }

            else -> {}
        }
    }

    fun setTabRedMark(index: Int, redMark: Boolean) {
        when (index) {
            0 -> {
                mViewBinding.bottomTabBarItem0.setRedMark(redMark)
                mViewBinding.bottomTabBarItem1.setRedMark(false)
                mViewBinding.bottomTabBarItem2.setRedMark(false)
            }

            1 -> {
                mViewBinding.bottomTabBarItem0.setRedMark(false)
                mViewBinding.bottomTabBarItem1.setRedMark(redMark)
                mViewBinding.bottomTabBarItem2.setRedMark(false)
            }

            2 -> {
                mViewBinding.bottomTabBarItem0.setRedMark(false)
                mViewBinding.bottomTabBarItem1.setRedMark(false)
                mViewBinding.bottomTabBarItem2.setRedMark(redMark)
            }

            else -> {}
        }
    }
}